package com.neusoft.databus.common.dto;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;

import com.neusoft.bizcore.web.dto.AbstractAuditDTO;
import com.neusoft.databus.common.enums.MetricUsageType;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * description
 *
 * @author sunchf
 * @date 2018年12月21日 下午4:33:16
 */
@Getter
@Setter
@ToString
public class ResourceDTO extends AbstractAuditDTO {
    private static final long serialVersionUID = -7327904291063692903L;

    /** 资源编码唯一标识, 根据ID生成器生成 */
    @ApiModelProperty(value = "资源编码", required = false, position = 1)
    private String code;

    /** 资源名称 */
    @ApiModelProperty(value = "资源名称", required = true, position = 2)
    @NotBlank(message = "资源名称不能为空")
    @Length(max = 255, message = "资源名称不能超过{max}位")
    private String name;

    /** 禁用标识 */
    @ApiModelProperty(value = "禁用标识", required = false, position = 3)
    private boolean disabled = false;

    /** 资源配置属性 */
    @ApiModelProperty(value = "资源配置属性", required = false, position = 5)
    @Length(max = 1023, message = "资源配置属性不能超过{max}位")
    private String config;

    /** 网关编码 */
    @ApiModelProperty(value = "网关编码", required = false, position = 6)
    private String gatewayCode;

    /** 资源类型 */
    @Length(max = 255, message = "资源类型不能超过{max}位")
    @ApiModelProperty(value = "资源类型", required = false, position = 7)
    private String type;

    /** 网关信息 */
    @ApiModelProperty(value = "网关信息", required = false, position = 8)
    private GatewayDTO gateway;

    /** 关联指标组数量(列表查询时, 只显示指标数量) */
    @ApiModelProperty(value = "关联指标组量", required = false, position = 9)
    private int metricGroupSize = 0;

    /** 关联指标组 */
    @ApiModelProperty(value = "关联指标组", required = false, position = 10)
    private List<MetricGroupDTO> metricGroups = new ArrayList<>();

    @ApiModelProperty(value = "子系统关联关系", required = false, position = 11)
    private String associate;

    /** 注释 */
    @ApiModelProperty(value = "注释", required = false, position = 12)
    private String comment;

    /**
     * 采集周期(任务调度用)
     */
    @NotNull(message = "采集周期不能为空")
    @Min(value = 1, message = "采集周期不能小于{value}秒")
    private Long executionCycle = 60L;// 指标组的采集周期

    public boolean containCollectedMetricGroup() {
        return this.metricGroups.stream().filter(it -> MetricUsageType.COLLECT.equals(it.getUsageType())).count() > 0;
    }

    public MetricGroupDTO getCollectedMetricGroup() {
        final Optional<MetricGroupDTO> opt =
                this.metricGroups.stream().filter(it -> MetricUsageType.COLLECT.equals(it.getUsageType())).findFirst();
        return opt.isPresent() ? opt.get() : null;
    }
}
